#!/usr/bin/env perl

use Getopt::Std;

my $debug = 0;
my %fshash = ();

sub usage {
	print "[+] ./check_sync.pl \n" if $debug;
	print "[+]\t\t -c count (Maximum acceptable snapshots / filesystem)\n\n" if $debug;
}

getopt('c'); $maxallowed = $opt_c;

if ($maxallowed !~ /^\d+$/) { usage(); print "3\n"; exit 1; }


open(ZFSLIST, "/usr/sbin/zfs list -t snapshot |");
while (<ZFSLIST>) {
	next if ($_ =~ /NAME/);
	@fstmp = split('@',$_); @snptmp = split(' ',$fstmp[1]); $fshash{$fstmp[0]}++;
}
close(ZFSLIST);

while ( my ($fs, $snpcount) = each(%fshash) ) {
	print "[F] $fs [S] $snpcount\n" if $debug;
	if ($snpcount > $maxallowed) { print "0\n"; exit 0 } 
}

print "1\n";
